const query = require('./query')

const insertCartList = async ({ cartList }) => {
  const sql = `insert into 
  cart(cart_id,goodsInfo_id,goods_count,goods_checked,user_id) 
  values ?
  on duplicate key update 
  goods_count = values(goods_count), 
  goods_checked = values(goods_checked)
  `
  try {
    const result = await query(sql, [cartList])
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}
const queryCartListByUserId = async (user_id) => {
  const sql = `select * from cart where user_id = ? and cart_status = 1`
  try {
    const result = await query(sql, user_id)
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}
const deleteCartByGoodsInfoIdAndUserId = async (goodsInfo_id, user_id) => {
  const sql = `delete from cart where goodsInfo_id = ? and user_id = ?`
  try {
    const result = await query(sql, [goodsInfo_id, user_id])
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}
const deleteCartAllByUserId = async (user_id) => {
  const sql = `delete from cart where user_id = ?`
  try {
    const result = await query(sql, user_id)
    return result
  } catch (error) {
    return Promise.reject(error)
  }
}
module.exports = {
  insertCartList,
  queryCartListByUserId,
  deleteCartByGoodsInfoIdAndUserId,
  deleteCartAllByUserId
}